package dao.csv;

import java.sql.Connection;

import model.Film;
import model.ModelBase;
import dao.AbstractRecordDAO;



public class FilmCsvRecordDAO extends AbstractRecordDAO<Film>
{

  private final char seperator;
  private String lineFormat = "%d;%s;%s;%s;%d";

  public FilmCsvRecordDAO() 
  {
    this(';');
  }

  public FilmCsvRecordDAO(char c) 
  {
    seperator = c;
  }

  @Override
  protected String build(Film film) 
  {
    if (seperator != ';') 
    {
      lineFormat = lineFormat.replace(';', seperator);
    }
    return String.format(lineFormat, film.getId(), film.getName().trim(), film.getHauptdarsteller().trim(), film.getGenre(), film.getLaenge());
  }

  @Override
  protected Film parse(String line) 
  {
    String[] columns = line.split(";");
    if (columns.length != 5) 
    { 
    	throw new IllegalArgumentException(String.format("wrong format in line: %s", line)); 
    }
    try 
    {
      return new Film(Long.valueOf(columns[0]), columns[1], columns[2], columns[3], Integer.valueOf(columns[4]));
    }
    catch (NumberFormatException e) 
    {
      throw new IllegalArgumentException(String.format("wrong format in laenge (%s)", columns[4]), e);
    }
  }

  @Override
  public int save(Connection con, ModelBase data) 
  {
	// TODO Auto-generated method stub
	return 0;
  }

  @Override
  public int delete(Connection con, ModelBase data) 
  {
	// TODO Auto-generated method stub
	return 0;
  }
}
